# -*- coding: utf-8 -*-
# flake8: noqa

from qiniu import Auth, put_file, etag
import qiniu.config
import sys
import time

defaultencoding = 'utf-8'
if sys.getdefaultencoding() != defaultencoding:
    reload(sys)
    sys.setdefaultencoding(defaultencoding)
#需要填写你的 Access Key 和 Secret Key
access_key = 'KkWyXJ0ywFlIjwR219WxImET1uVo3oGl0uFtHWv4'
secret_key = 'u1S-50YglqUzwS8KIPPDR10kwPKTg3a7RuvJJlqi'

#构建鉴权对象
q = Auth(access_key, secret_key)

#要上传的空间
bucket_name = 'macro-zwy'

#上传后保存的文件名
t = time.time()
lens=len(sys.argv)


if lens==3:
    key = sys.argv[1]+str(round(t * 1000))
elif lens==2:
    key=str(round(t * 1000))
else:
    print('参数错误')
    sys.exit(0)

#生成上传 Token，可以指定过期时间等
token = q.upload_token(bucket_name, key, 3600)

#要上传文件的本地路径
if lens==3:
    localfile = sys.argv[2]
elif lens==2:
    localfile = sys.argv[1]

ret, info = put_file(token, key, localfile, version='v2')

print('http://cdn.onespace.asia/'+str(key))
assert ret['key'] == key
assert ret['hash'] == etag(localfile)

